/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2022-06-22 11:48:25
 * @LastEditors: TT
 * @LastEditTime: 2022-07-11 16:13:48
 */

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import 'package:hzy_template/config/dataconfig/language_info_config.dart';

import 'common_sp_util.dart';

class ThemeTool {
  /// 切换主题
  static changeTheme({int? type = 1}) {
    ThemeMode mode = getlocalprofileaboutThemeModel();
    ThemeData themeData = getlocalprofileaboutThemeData();
    EasyLoadingStyle easyLoadingStyle = EasyLoadingStyle.dark;
    if (mode == ThemeMode.dark) {
      easyLoadingStyle = EasyLoadingStyle.light;
    } else if (mode == ThemeMode.system) {
      if (!Get.isDarkMode) {
        easyLoadingStyle = EasyLoadingStyle.light;
      }
    }
    EasyLoading.instance.loadingStyle = easyLoadingStyle;
    Get.changeThemeMode(mode);
    Get.changeTheme(themeData);

    updateTheme();
  }

  static updateTheme() {
    Future.delayed(const Duration(milliseconds: 250), () {
      Get.forceAppUpdate();
    });
  }

  /// 获取本地 主题配置
  static getlocalprofileaboutThemeModel() {
    int theme = CommonSpUtil.getThemeType();
    ThemeMode themeMode = ThemeMode.light;
    if (theme == 0) {
      themeMode = ThemeMode.light;
    } else if (theme == 1) {
      themeMode = ThemeMode.dark;
    } else if (theme == 2) {
      themeMode = ThemeMode.system;
    }
    return themeMode;
  }

  /// 获取 主题配置 开启状态
  static getthemetype() {
    String msg = LauchNormalMsg.notopen;
    int theme = CommonSpUtil.getThemeType();
    if (theme == 0) {
      msg = LauchNormalMsg.notopen;
    } else if (theme == 1) {
      msg = LauchNormalMsg.opened;
    } else if (theme == 2) {
      msg = LauchNormalMsg.themesystem;
    }
    return msg;
  }

  static getlocalprofileaboutThemeData() {
    int theme = CommonSpUtil.getThemeType();
    ThemeData themeData = ThemeData.light();
    if (theme == 0) {
      themeData = ThemeData(brightness: Brightness.light);
    } else if (theme == 1) {
      themeData = ThemeData(brightness: Brightness.dark);
    } else if (theme == 2) {
      if (!Get.isDarkMode) {
        themeData = ThemeData(brightness: Brightness.dark);
      } else {
        themeData = ThemeData(brightness: Brightness.light);
      }
    }
    return themeData;
  }

  static isdark() {
    int type = CommonSpUtil.getThemeType();
    if (type == 2) {
      return MediaQuery.of(Get.context!).platformBrightness == Brightness.dark;
    } else {
      return Get.isDarkMode;
    }
  }
}
